/* 
 * Copyright (c), nFractals Ltd. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.parade.driver;


import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import org.parade.distributed.Id;


/*
 * Provides interface for the most basic distributed primitives.
 * 
 * 
 * 
 * @author		Lee moon soo
 * 
 * @version		%I%, %G%
 * 
 * @since		1.0
 * 
 */
public abstract class Driver {
	
	private ThreadExecutionService threadExecutionService;


	public Driver(){
	}
	
	public ThreadExecutionService getThreadExecutionService(){
		startThreadExecutionService();
		return threadExecutionService;
		
	}
	
	public void startThreadExecutionService(){
		if(threadExecutionService==null){
			this.threadExecutionService = new ThreadExecutionService(this);
			this.threadExecutionService.start();
		}
	}
	
	public void destroy(){
		this.threadExecutionService.terminate();
	}
	
	/**
	 * Get cluster shared lock
	 * 
	 * @param	id	cluster unique id
	 *  
	 * @return	Distributed lock object which implements java.util.concurrent.locks.Lock
	 */
	public abstract Lock getLock(Id id);
	

	/**
	 * Create condition related with given lock
	 * 
	 * @param lock
	 * @return
	 */
	public abstract Condition getCondition(org.parade.distributed.Lock lock);
	
	
	/**
	 * creates cluste wide unique. Generated id are long type primitive
	 * Generated ID are unique during the life cycle of application
	 * 
	 * @return	cluster wide unique number between 0 and Long.MAX_VALUE
	 */
	public abstract long generateUniqueId();
			
	/**
	 * Create distributed blocking queue
	 * 
	 * @param id	cluster unique id
	 * 
	 * @return		blocking queue
	 */
	public abstract BlockingQueue getBlockingQueue(Id id);
	
	
	/**
	 * Create distributed map
	 * 
	 * @param id	cluster unique id
	 * @return		map
	 */
	public abstract Map getMap(Id id);
	
	
	
}
